package com.arcanite.dailyplanner.view.activity;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.arcanite.dailyplanner.R;
import com.arcanite.dailyplanner.model.util.CommonUtils;
import com.arcanite.dailyplanner.view.fragment.DailyFragment;
import com.arcanite.dailyplanner.view.fragment.HomeFragment;


public class MainActivity extends AppCompatActivity {
    private final static int[] TITLE_ID = new int[]{
            R.string.sunday, R.string.monday, R.string.tuesday,
            R.string.wednesday, R.string.thursday,
            R.string.friday, R.string.saturday};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initActionBar();
        initView();
    }

    private void initActionBar() {
        ActionBar actionBar = super.getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(false);
        }
    }

    private void initView() {
        HomeFragment mFragment = HomeFragment.newInstance();
        for (int aTITLE_ID : TITLE_ID) {
            mFragment.addPage(getString(aTITLE_ID), DailyFragment.newInstance(aTITLE_ID));
        }
        CommonUtils.replaceFl2Fg(this.getSupportFragmentManager(), R.id.fl_content, mFragment);
    }

//    @Override
//    public boolean onMenuOpened(int featureId, Menu menu) {
//        if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
//            if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
//                try {
//                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
//                    m.setAccessible(true);
//                    m.invoke(menu, true);
//                }catch (Exception e){
//
//                }
//            }
//        }
//        return super.onMenuOpened(featureId, menu);
//    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            // 如果ActionBar设置setDisplayHomeAsUpEnabled为true，则可以监听到前面的箭头图标
            case android.R.id.home:
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}
